﻿using System; 
using System.Text;
using System.Collections.Generic; 
using System.Data;
using System.Data.SqlClient;
using ART.Data;
using ART.Common;
using ART.Model;
using ART.Cache;

namespace ART.BLL {
  //TBUsers
 	public partial class B_TBUsers
	{
   		
		public B_TBUsers()
		{}
        #region __________基础方法__________
		/// <summary>
		/// 删除指定缓存
		/// </summary>
		public void ClearCache(string key)
		{
		    string CacheKey = "BLL/TBUsers";
			if(!string.IsNullOrEmpty(key)){
			    CacheKey+="/"+key;
			}
			LRSCache.GetCacheService().RemoveObject(CacheKey);
		}
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(ART.Model.M_TBUsers model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into TBUsers(");			
            strSql.Append("Account,PWD,AID,UserType,UserGroupId,Enabled,CreateTime,LastLoginIP,LastLoginTime,LastLogoutTime,LoginTimes,UserFrom");
			strSql.Append(") values (");
            strSql.Append("@Account,@PWD,@AID,@UserType,@UserGroupId,@Enabled,@CreateTime,@LastLoginIP,@LastLoginTime,@LastLogoutTime,@LoginTimes,@UserFrom");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");	
			#region __________参数__________	
			SqlParameter[] parameters = {
			            new SqlParameter("@Account", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@PWD", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@AID", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserType", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserGroupId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Enabled", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@LastLoginIP", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@LastLoginTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@LastLogoutTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@LoginTimes", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserFrom", SqlDbType.VarChar,150)             
              
            };
			
			            
            parameters[0].Value = model.Account;                        
            parameters[1].Value = model.PWD;                        
            parameters[2].Value = model.AID;                        
            parameters[3].Value = model.UserType;                        
            parameters[4].Value = model.UserGroupId;                        
            parameters[5].Value = model.Enabled;                        
            parameters[6].Value = model.CreateTime;                        
            parameters[7].Value = model.LastLoginIP;                        
            parameters[8].Value = model.LastLoginTime;                        
            parameters[9].Value = model.LastLogoutTime;                        
            parameters[10].Value = model.LoginTimes;                        
            parameters[11].Value = model.UserFrom;            			
            #endregion
			   
			object obj = SqlHelper.ExecScalar(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
				                    
            	return Convert.ToInt32(obj);
                                                                  
			}			   
            		
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ART.Model.M_TBUsers model)
		{
		    StringBuilder strSql=new StringBuilder();
			#region __________语句__________
			strSql.Append("update TBUsers set ");
			                                                
            strSql.Append(" Account = @Account , ");                                    
            strSql.Append(" PWD = @PWD , ");                                    
            strSql.Append(" AID = @AID , ");                                    
            strSql.Append(" UserType = @UserType , ");                                    
            strSql.Append(" UserGroupId = @UserGroupId , ");                                    
            strSql.Append(" Enabled = @Enabled , ");                                    
            strSql.Append(" CreateTime = @CreateTime , ");                                    
            strSql.Append(" LastLoginIP = @LastLoginIP , ");                                    
            strSql.Append(" LastLoginTime = @LastLoginTime , ");                                    
            strSql.Append(" LastLogoutTime = @LastLogoutTime , ");                                    
            strSql.Append(" LoginTimes = @LoginTimes , ");                                    
            strSql.Append(" UserFrom = @UserFrom  ");            			
			strSql.Append(" where Id=@Id ");
			
			#endregion	
			
			#region __________参数__________	
            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@Account", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@PWD", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@AID", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserType", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserGroupId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Enabled", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@LastLoginIP", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@LastLoginTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@LastLogoutTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@LoginTimes", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserFrom", SqlDbType.VarChar,150)             
              
            };
            			            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.Account;                        
            parameters[2].Value = model.PWD;                        
            parameters[3].Value = model.AID;                        
            parameters[4].Value = model.UserType;                        
            parameters[5].Value = model.UserGroupId;                        
            parameters[6].Value = model.Enabled;                        
            parameters[7].Value = model.CreateTime;                        
            parameters[8].Value = model.LastLoginIP;                        
            parameters[9].Value = model.LastLoginTime;                        
            parameters[10].Value = model.LastLogoutTime;                        
            parameters[11].Value = model.LoginTimes;                        
            parameters[12].Value = model.UserFrom;            			
            #endregion
			
            int rows=SqlHelper.ExecNonQuery(strSql.ToString(),parameters);
			if (rows > 0)
			{
			//string cachekey="info_"+Id;
			//ClearCache(cachekey);
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TBUsers ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			int rows=SqlHelper.ExecNonQuery(strSql.ToString(),parameters);
			
			//string cachekey="info_"+Id;
			//ClearCache(cachekey);
			
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TBUsers ");
			strSql.Append(" where ID in ("+Idlist + ")  ");
			int rows=SqlHelper.ExecNonQuery(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

 	    /// <summary>
 	    /// 得到一个对象实体
 	    /// </summary>
 	    public ART.Model.M_TBUsers GetModel(int Id)
 	    {
            string CacheKey = "BLL/TBUsers/info_" + Id;
            object objModel = LRSCache.GetCacheService().RetrieveObject(CacheKey);
            if (objModel == null)
            {
 	        StringBuilder strSql = new StringBuilder();
 	        strSql.Append("select * from TBUsers ");
 	        strSql.Append(" where Id=@Id");

 	        SqlParameter[] parameters =
 	            {
 	                new SqlParameter("@Id", SqlDbType.Int, 4)
 	            };
 	        parameters[0].Value = Id;


 	        ART.Model.M_TBUsers model = new ART.Model.M_TBUsers();
 	        DataSet ds = SqlHelper.ExecDataSet(strSql.ToString(), parameters);

 	        if (ds.Tables[0].Rows.Count > 0)
 	        {
 	            #region __________实体赋值__________

 	            if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
 	            {
 	                model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
 	            }
 	            model.Account = ds.Tables[0].Rows[0]["Account"].ToString();
 	            model.PWD = ds.Tables[0].Rows[0]["PWD"].ToString();
 	            if (ds.Tables[0].Rows[0]["AID"].ToString() != "")
 	            {
 	                model.AID = int.Parse(ds.Tables[0].Rows[0]["AID"].ToString());
 	            }
 	            if (ds.Tables[0].Rows[0]["UserType"].ToString() != "")
 	            {
 	                model.UserType = int.Parse(ds.Tables[0].Rows[0]["UserType"].ToString());
 	            }
 	            if (ds.Tables[0].Rows[0]["UserGroupId"].ToString() != "")
 	            {
 	                model.UserGroupId = int.Parse(ds.Tables[0].Rows[0]["UserGroupId"].ToString());
 	            }
 	            if (ds.Tables[0].Rows[0]["Enabled"].ToString() != "")
 	            {
 	                model.Enabled = int.Parse(ds.Tables[0].Rows[0]["Enabled"].ToString());
 	            }
 	            if (ds.Tables[0].Rows[0]["CreateTime"].ToString() != "")
 	            {
 	                model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
 	            }
 	            model.LastLoginIP = ds.Tables[0].Rows[0]["LastLoginIP"].ToString();
 	            if (ds.Tables[0].Rows[0]["LastLoginTime"].ToString() != "")
 	            {
 	                model.LastLoginTime = DateTime.Parse(ds.Tables[0].Rows[0]["LastLoginTime"].ToString());
 	            }
 	            if (ds.Tables[0].Rows[0]["LastLogoutTime"].ToString() != "")
 	            {
 	                model.LastLogoutTime = DateTime.Parse(ds.Tables[0].Rows[0]["LastLogoutTime"].ToString());
 	            }
 	            if (ds.Tables[0].Rows[0]["LoginTimes"].ToString() != "")
 	            {
 	                model.LoginTimes = int.Parse(ds.Tables[0].Rows[0]["LoginTimes"].ToString());
 	            }
                model.UserFrom = ds.Tables[0].Rows[0]["UserFrom"].ToString();


 	            #endregion

                LRSCache.GetCacheService().AddObject(CacheKey, model, 2 * 60 * 60);		
 	            return model;
 	        }
 	        else
 	        {
 	            return null;
 	        }

 	        }
 	        return (ART.Model.M_TBUsers)objModel;
 	    }

 	    /// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return GetList(0,strWhere,"");
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return GetList(0,"*",strWhere,filedOrder);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string FieldName,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" "+FieldName+" FROM TBUsers ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			if(filedOrder.Trim()!="")
			{
				strSql.Append(" order by " + filedOrder);
			}
			return SqlHelper.ExecDataSet(strSql.ToString());
		}
#endregion

      #region __________自定义方法__________

      #endregion 
	}
}

